package com.prs.crm.action.oa;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.oa.MailBox;
import com.prs.crm.domain.sys.MailAddress;
import com.prs.crm.service.oa.MailBoxService;
import com.prs.crm.service.sys.MailAddressService;

public class MailBoxAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private MailBoxService mailBoxService;

	private MailAddressService mailAddressService;

	private MailBox box;

	private Collection<MailAddress> addresses;

	private Collection<MailBox> boxes;

	private Integer[] boxIds;

	public void setMailAddressService(MailAddressService mailAddressService) {
		this.mailAddressService = mailAddressService;
	}

	public MailAddressService getMailAddressService() {
		return mailAddressService;
	}

	public void setMailBoxService(MailBoxService mailBoxService) {
		this.mailBoxService = mailBoxService;
	}

	public MailBoxService getMailBoxService() {
		return mailBoxService;
	}

	public void setBoxIds(Integer[] boxIds) {
		this.boxIds = boxIds;
	}

	public Integer[] getBoxIds() {
		return boxIds;
	}

	public void setBox(MailBox box) {
		this.box = box;
	}

	public MailBox getBox() {
		return box;
	}

	public void setBoxes(Collection<MailBox> boxes) {
		this.boxes = boxes;
	}

	public Collection<MailBox> getBoxes() {
		return boxes;
	}

	public void setAddresses(Collection<MailAddress> addresses) {
		this.addresses = addresses;
	}

	public Collection<MailAddress> getAddresses() {
		return addresses;
	}

	@Action("listMailBox")
	public String listMailBox() {
		this.setBoxes(this.getMailBoxService().findBoxes(
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("editMailBox")
	public String editMailBox() {
		this.setAddresses(this.getMailAddressService().getAllAddress());
		return SUCCESS;
	}

	@Action(value = "saveMailBox", results = {
			@Result(name = "success", type = "redirect", location = "listMailBox"),
			@Result(name = "error", type = "chain", location = "editMailBox") })
	public String saveMailBox() {
		if (box.getAddr() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (box.getUserName() == null || "".equals(box.getUserName().trim())) {
			addActionError("用户名不能为空");
			return ERROR;
		}
		if (box.getPassWord() == null || "".equals(box.getPassWord().trim())) {
			addActionError("密码不能为空");
			return ERROR;
		}

		this.getMailBoxService().save(box);
		return SUCCESS;
	}

	@Action(value = "removeMailBox", results = { @Result(name = "success", type = "redirect", location = "listMailBox") })
	public String removeMailBox() {
		this.getMailBoxService().batchRemove(boxIds);
		return SUCCESS;
	}

}
